#include <sqlite3.h>
#include <string>
#include <iostream>

class SqliteHelper
{
public:
    SqliteHelper(const std::string& dbfile)
        :_dbfile(dbfile), _handler(nullptr)
    {}
    bool Open(int flags = SQLITE_OPEN_FULLMUTEX)
    {
        flags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
        int ret = sqlite3_open_v2(_dbfile.c_str(), &_handler, flags, nullptr);
        if(ret != SQLITE_OK)
        {
            std::cout << "文件创建或者打开失败" << sqlite3_errmsg(_handler) << std::endl;
            return false;
        }
        return true;
    }
    bool Exec(const std::string& sql, sqlite3_callback cb = nullptr, void* arg = nullptr)
    {
        int ret = sqlite3_exec(_handler, sql.c_str(), cb, arg, nullptr);
        if(ret != SQLITE_OK)
        {
            std::cout << "sql执行失败" << sqlite3_errmsg(_handler) << std::endl;
            return false;
        }
        return true;
    }
    bool Close()
    {
        if(_handler) sqlite3_close(_handler);
        return true;
    }   
private:
    std::string _dbfile;
    sqlite3* _handler;
};